#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 22 16:57:19 2022

@author: mac
"""

import PySimpleGUI as sg
from main import ApiGenerator
from main import observablefield
from main import UICache


class MainUI(object):
    
    def __init__(self):
        self.uiCache = UICache("ui_api.txt")
        pass
    
    def show(self):
        sg.theme('DarkAmber')   # 设置当前主题
        # 界面布局，将会按照列表顺序从上往下依次排列，二级列表中，从左往右依此排列
        readCache = self.uiCache.load()
        default_api = ""
        default_params = ""
        default_comment = ""
        default_name = ""
        try:
            default_api = readCache["api"]
            default_params = readCache["params"]
            default_comment = readCache["comment"]
            default_name = readCache["name"]
        except Exception as e:
            pass
        self.layout = [[sg.Text('api(*)'),sg.InputText(key='api',default_text=default_api)],
                    [sg.Text('params(*)'), sg.InputText(default_text=default_params,key='param')],
                    [sg.Text('comment(*)'), sg.InputText(default_text=default_comment,key='comment')],
                    [sg.Text('name'), sg.InputText(default_text=default_name,key='name')],
                    [sg.Button('Ok'), sg.Button('Cancel')] ]
         

        # 创造窗口
        self.window = sg.Window('接口代码生成神器', self.layout)

        self.api = observablefield(self.set_api, self.get_api)
        self.params = observablefield(self.set_params, self.get_params)
        self.comment = observablefield(self.set_comment, self.get_comment)
        self.name = observablefield(self.set_name, self.get_name)

        # 事件循环并获取输入值
        while True:
            event, values = self.window.read()
            if event in (None, 'Cancel'):   # 如果用户关闭窗口或点击`Cancel`
                break
            
            if self.is_valid():
                obj = {}
                obj["api"] = self.api.get()
                obj["params"] = self.params.get()
                obj["comment"] = self.comment.get()
                obj["name"] = self.name.get()
                self.uiCache.write(obj)
                ApiGenerator(api=self.api.get(),
                             params=self.params.get(),
                             comment=self.comment.get(), 
                             name=self.name.get()).generate()
                sg.popup_ok('已生成')
            else:
                sg.popup_ok('请输入参数')
         
        self.window.close()
        
    def is_valid(self):
        if self.api.get() == "":
            return False
        
        return True

    # 定义 databinding 的 setter, getter 回调
    def set_api(self,str):
        self.window["api"].update(value=str)
        
    def get_api(self):
        return self.window["api"].get()
    
    def set_params(self,str):
        self.window["param"].update(value=str)
        
    def get_params(self):
        return self.window["param"].get()
   
    def set_comment(self,str):
        self.window["comment"].update(value=str)
        
    def get_comment(self):
        return self.window["comment"].get()
    
    def set_name(self,str):
        self.window["name"].update(value=str)
        
    def get_name(self):
        return self.window["name"].get()
    

MainUI().show()
